<template>
	<view class="page-body " :class="[app.setCStyle()]">
		<view class="main-box  base-select">
			<view class="navbar">
				<view class="nav-item" :class="{current: tabCurrentIndex === 0}" @click="tabClick(0)">直推下级</view>
				<view class="nav-item" :class="{current: tabCurrentIndex === 1}" @click="tabClick(1)">间推下级</view>
				<view class="nav-item" :class="{current: tabCurrentIndex === 2}" @click="tabClick(2)">树状图</view>
			</view>

			<swiper :current="tabCurrentIndex" class="swiper-box" duration="300" @change="changeTab">
				<swiper-item class="tab-content">
					<view class="p20">
						<view class="collapse_item" :index="index" @click="collapseChange(index)" v-for="(item, index) in directList"
						 :key="index">
							<view class="title">
								{{item.role_name}}
								<text class="count">({{item.count}})</text>
								<view class="fr">
									<view v-if="item.show == 1">
										<u-icon name="arrow-up" color="#666666" size="20"></u-icon>
									</view>
									<view v-else>
										<u-icon name="arrow-down" color="#666666" size="20"></u-icon>
									</view>
								</view>
							</view>
							<view class="user_box" v-if="item.show == 1">
								<view class="user_item space-between" v-for="(uItem, uIndex) in item.userList" :key="uIndex">
									<view>
										<view class="img">
											<u-image :src="uItem.headimgurl?baseUrl+uItem.headimgurl:'/static/public/images/headimgurl.jpg'" mode="widthFix"
											 shape="circle"></u-image>
										</view>
										<text class="fs28">{{uItem.nick_name}}</text>
									</view>
									<view class=" fs28 color-66">{{uItem.mobile}}</view>
								</view>
							</view>
						</view>
					</view>
				</swiper-item>
				<swiper-item class="tab-content">
					<view class="p20">
						<view class="collapse_item" :index="index" @click="collapseChange(index)" v-for="(item, index) in indirectList"
						 :key="index">
							<view class="title">
								{{item.role_name}}
								<text class="count">({{item.count}})</text>
								<view class="fr">
									<view v-if="item.show == 1">
										<u-icon name="arrow-up" color="#666666" size="20"></u-icon>
									</view>
									<view v-else>
										<u-icon name="arrow-down" color="#666666" size="20"></u-icon>
									</view>
								</view>
							</view>
							<view class="user_box" v-if="item.show == 1">
								<view class="user_item  space-between" v-for="(uItem, uIndex) in item.userList" :key="uIndex">
									<view>
										<view class="img">
											<u-image :src="uItem.headimgurl?baseUrl+uItem.headimgurl:'/static/public/images/headimgurl.jpg'" mode="widthFix"
											 shape="circle"></u-image>
										</view>
										<text class="fs28">{{uItem.nick_name}}</text>
									</view>
									<view class="fs28 color-66">{{uItem.mobile}}</view>
								</view>
							</view>
						</view>
					</view>
				</swiper-item>
				<swiper-item class="tab-content">
					<view class="p20 team-tree">
						<view class="all-title">
							全部<text class="count">({{teamCount}})</text>
							<view class="icon">
								<u-icon name="arrow-right" size="20"></u-icon>
							</view>
						</view>
						<view>
							<tempSub v-if="teamCount > 0" :pid="0" :pl="0" :underUserlist="underUserlist"></tempSub>
						</view>
					</view>
				</swiper-item>
			</swiper>

		</view>
	</view>
</template>

<script>
	import tempSub from '@/pages/member/team/tempSub';
	export default {
		components: {
			tempSub
		},
		data() {
			return {
				baseUrl: this.config.baseUrl,
				tabCurrentIndex: 0,
				directList: {}, //直推
				indirectList: {}, //间推
				teamCount: 0,
				underUserlist: {}
			}
		},
		onLoad(options) {
			if (typeof(options.tabCurrentIndex) != 'undefined') {
				this.tabCurrentIndex = options.tabCurrentIndex;
			}
			this.getTeamCount(); //默认获取直推
		},
		methods: {
			//顶部tab点击

			tabClick(index) {
				this.tabCurrentIndex = index;
			},
			//swiper 切换
			changeTab(e) {
				this.tabCurrentIndex = e.target.current;
				this.getTeamCount();
			},
			collapseChange(index) { //查看列表
				this.getUserList(index);
			},
			getTeamCount() {
				if (this.tabCurrentIndex == 0) { //直推
					if (this.directList.length > 0) {
						return false;
					}
					this.$u.post('member/api.team/getTeamCount', {
						type: 'direct'
					}).then(res => {
						this.directList = res.data;
					})
				} else if (this.tabCurrentIndex == 1) { //间推
					if (this.indirectList.length > 0) {
						return false;
					}
					this.$u.post('member/api.team/getTeamCount', {
						type: 'indirect'
					}).then(res => {
						this.indirectList = res.data;
					})
				} else { //树状图
					if (this.underUserlist.length > 0) {
						return false;
					}
					this.$u.post('member/api.team/getUnderUserlist').then(res => {
						this.teamCount = res.data.count;
						this.underUserlist = res.data.list;
					})
				}

			},
			getUserList(index) {
				if (this.tabCurrentIndex == 0) { //直推
					let directList = this.directList[index];
					if (directList.count < 1) {
						return false;
					}
					if (directList.show == 1) {
						directList.show = 0;
						this.$forceUpdate()
						return false;
					} else {
						directList.show = 1;
					}
					if (typeof(directList.userList) != 'undefined') {
						this.$forceUpdate()
						return false;
					}
					this.$u.post('member/api.team/getTeamUserList', {
						type: 'direct',
						role_id: directList.role_id
					}).then(res => {
						directList.userList = res.data;
						this.$set(directList, 'loaded', true);
					})
				} else if (this.tabCurrentIndex == 1) { //间推
					let indirectList = this.indirectList[index];
					if (indirectList.count < 1) {
						return false;
					}
					if (indirectList.show == 1) {
						indirectList.show = 0;
						this.$forceUpdate()
						return false;
					} else {
						indirectList.show = 1;
					}
					if (typeof(indirectList.userList) != 'undefined') {
						this.$forceUpdate()
						return false;
					}
					this.$u.post('member/api.team/getTeamUserList', {
						type: 'indirect',
						role_id: indirectList.role_id
					}).then(res => {
						indirectList.userList = res.data;
						this.$set(indirectList, 'loaded', true);

					})
				}
			}
		}
	}
</script>

<style lang="scss">
	.main-box {
		height: calc(100% - 80rpx);
	}

	.swiper-box {
		height: calc(100% - 40rpx);
	}

	.tab-content {
		overflow-y: auto;
	}

	.collapse_item {
		display: block;
		position: relative;
		width: 100%;
		padding: 0 20rpx;
		background-color: #ffffff;
		margin-bottom: 20rpx;
		box-shadow: 0px 10rpx 20rpx 0px rgba(0, 0, 0, 0.04);
		border-radius: 0px 0px 10rpx 10rpx;

		.title {
			line-height: 100rpx;
			color: $font-color-dark;
			font-size: 28rpx;

			.count {
				color: $font-color-dark;
				font-size: 28rpx;
				margin-left: 5rpx;
			}
		}
	}

	.user_box {

		.user_item {
			height: 100rpx;
			line-height: 100rpx;

			.img {
				float: left;
				margin-right: 10rpx;
				width: 80rpx;
				height: 80rpx;
				border-radius: 50%;
			}

		}
	}

	.team-tree {

		.all-title {
			position: relative;
			width: 100%;
			padding: 0 20rpx;
			background-color: #ffffff;
			margin-bottom: 20rpx;
			box-shadow: 0px 10rpx 20rpx 0px rgba(0, 0, 0, 0.04);
			border-radius: 0px 0px 10rpx 10rpx;
			line-height: 100rpx;
			color: $font-color-dark;
			font-size: 28rpx;

			.count {
				color: #98979a;
				font-size: 28rpx;
				margin-left: 5rpx;
			}

			.icon {
				float: right;
				color: rgb(144, 147, 153);
				font-size: inherit;
			}
		}
	}
</style>
